package gameMiniGames;


import gameAudio.Audio;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

public class CircuitButton extends JLayeredPane {	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	Object[] a = new Object[2];
	
	int roomValue = 101;
	//File file = new File("src/gameMiniGames/gameMusic1.mid");
	//Audio aud = new Audio();
	
	public boolean firstTime = true;
	public boolean played = false;

	//JLayeredPane layeredPane;
	ImageIcon background = new ImageIcon("src/gameMiniGames/CircuitGame/back.jpg");
	ImageIcon oneToOne = new ImageIcon("src/gameMiniGames/CircuitGame/1to1.png");
	ImageIcon oneToTwo = new ImageIcon("src/gameMiniGames/CircuitGame/1to2.png");
	ImageIcon oneToThree = new ImageIcon("src/gameMiniGames/CircuitGame/1to3.png");
	ImageIcon twoToOne = new ImageIcon("src/gameMiniGames/CircuitGame/2to1.png");
	ImageIcon twoToTwo = new ImageIcon("src/gameMiniGames/CircuitGame/2to2.png");
	ImageIcon twoToThree = new ImageIcon("src/gameMiniGames/CircuitGame/2to3.png");
	ImageIcon threeToOne = new ImageIcon("src/gameMiniGames/CircuitGame/3to1.png");
	ImageIcon threeToTwo = new ImageIcon("src/gameMiniGames/CircuitGame/3to2.png");
	ImageIcon threeToThree = new ImageIcon("src/gameMiniGames/CircuitGame/3to3.png");
	ImageIcon start = new ImageIcon("src/gameMiniGames/CircuitGame/start.png");
	
	JLabel wireStart = new JLabel(start);
	JLabel label = new JLabel();
	JLabel label2 = new JLabel();
	JLabel label3 = new JLabel();
	JLabel bground = new JLabel(background);
	JButton switchOne = new JButton("Switch One");
	JButton switchTwo = new JButton("Switch Two");
	JButton switchThree = new JButton("Switch Three");
	JButton wireOne = new JButton("Wire One");
	JButton wireTwo = new JButton("Wire Two");
	JButton wireThree = new JButton("Wire Three");
	JButton Start = new JButton("Start");
	Color btnDefault;
	Color btnSecond = new Color(112,138,144);
	Color green = new Color(0,255,0);
	Color red = new Color(255,0,0);
	
	public boolean[] onOff = new boolean[3];
	
	public CircuitButton(){
		
		for(int i = 0; i < onOff.length; i++){
			onOff[i] = false;
		}
		
		//aud.playMusic(file, roomValue);
		
		//layeredPane = getLayeredPane();
		bground.setBounds(0,0,640,452);
		wireStart.setBounds(75,125,490,200);
		switchOne.setBounds(75,85,163,40);
		switchTwo.setBounds(238,85,163,40);
		switchThree.setBounds(401,85,163,40);
		wireOne.setBounds(75,325,163,40);
		wireTwo.setBounds(238,325,163,40);
		wireThree.setBounds(401,325,163,40);
		Start.setBounds(238,385,163,40);
		
		add(wireStart, new Integer(4));	
		add(bground,JLayeredPane.DEFAULT_LAYER);
		add(switchOne,new Integer(3));
		add(switchTwo,new Integer(3));
		add(switchThree,new Integer(3));
		add(wireOne,new Integer(3));
		add(wireTwo,new Integer(3));
		add(wireThree,new Integer(3));
		add(Start,new Integer(3));
		
		btnDefault = Start.getBackground();

		wireOne.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event){
				if(wireOne.getBackground().equals(btnDefault)){
					wireOne.setBackground(btnSecond);
				}
				else{
					wireOne.setBackground(btnDefault);
				}
				check(a, wireOne);
			}		
		});

		wireTwo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event){
				if(wireTwo.getBackground().equals(btnDefault)){
					wireTwo.setBackground(btnSecond);
				}
				else{
					wireTwo.setBackground(btnDefault);
				}
				check(a, wireTwo);
			}				
		});
		wireThree.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event){
				if(wireThree.getBackground().equals(btnDefault)){
					wireThree.setBackground(btnSecond);
				}
				else{
					wireThree.setBackground(btnDefault);
				}
				check(a, wireThree);
			}				
		});
		switchOne.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event){
				new Thread(new Runnable(){						
					public void run(){
						SwingUtilities.invokeLater(new Runnable(){
							public void run(){
								if(a[0] != null){	
									if(switchOne.getBackground().equals(btnDefault)){
										switchOne.setBackground(btnSecond);
									}
									else{
										switchOne.setBackground(btnDefault);
									}
									checking(a, switchOne);
								}
							}
						});
					}
				}).start();
			}		
		});
		switchTwo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event){
				new Thread(new Runnable(){						
					public void run(){
						SwingUtilities.invokeLater(new Runnable(){
							public void run(){
								if(a[0] != null){
									if(switchTwo.getBackground().equals(btnDefault)){
										switchTwo.setBackground(btnSecond);
									}
									else{
										switchTwo.setBackground(btnDefault);
									}
									checking(a, switchTwo);
								}	
							}
						});
					}
				}).start();
			}		
		});
		switchThree.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event){
				new Thread(new Runnable(){						
					public void run(){
						SwingUtilities.invokeLater(new Runnable(){
							public void run(){
								if(a[0] != null){
									if(switchThree.getBackground().equals(btnDefault)){
										switchThree.setBackground(btnSecond);
									}
									else{
										switchThree.setBackground(btnDefault);
									}
									checking(a, switchThree);
								}	
							}
						});
					}
				}).start();
			}		
		});
		
		Start.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event){
				new Thread(new Runnable(){						
					public void run(){
						SwingUtilities.invokeLater(new Runnable(){
							public void run(){
								tutorial();
							}
						});
					}
				}).start();
			}		
		});
			
	}
	public void check(Object[] a, JButton wire){
		a[0] = wire;
	}
	public void checking(Object[] a, JButton switches){
		a[1] = switches;
		remove(wireStart);
		
		if(a[0] == wireOne && a[1] == switchOne){
			remove(label);
			label.removeAll();
			label.setBounds(75,125,490,200);
			label.setIcon(oneToOne);
			add(label, new Integer(4));
		}
		if(a[0] == wireOne && a[1] == switchTwo){
			//switchTwo.setBackground(red);
			remove(label);
			label.removeAll();
			label.setBounds(75,125,490,200);
			label.setIcon(oneToTwo);
			add(label, new Integer(4));
		}
		if(a[0] == wireOne && a[1] == switchThree){
			remove(label);
			label.removeAll();
			label.setBounds(75,125,490,200);
			label.setIcon(oneToThree);
			add(label, new Integer(4));
			switchThree.setBackground(green);
			wireOne.setEnabled(false);
			switchThree.setEnabled(false);
			a[0] = null;
			onOff[0] = true;
		}
		
		
		if(a[0] == wireTwo && a[1] == switchOne){
			remove(label2);
			label2.removeAll();
			label2.setBounds(75,125,490,200);
			label2.setIcon(twoToOne);
			add(label2, new Integer(4));
			switchOne.setBackground(green);
			wireTwo.setEnabled(false);
			switchOne.setEnabled(false);
			a[0] = null;
			onOff[1] = true;
		}
		if(a[0] == wireTwo && a[1] == switchTwo){
			//switchTwo.setBackground(red);
			remove(label2);
			label2.removeAll();
			label2.setBounds(75,125,490,200);
			label2.setIcon(twoToTwo);
			add(label2, new Integer(4));
		}
		if(a[0] == wireTwo && a[1] == switchThree){
			//switchThree.setBackground(green);
			remove(label2);
			label2.removeAll();
			label2.setBounds(75,125,490,200);
			label2.setIcon(twoToThree);
			add(label2, new Integer(4));
		}
		
		
		if(a[0] == wireThree && a[1] == switchOne){
			//switchOne.setBackground(red);
			remove(label3);
			label3.removeAll();
			label3.setBounds(75,125,490,200);
			label3.setIcon(threeToOne);
			add(label3, new Integer(4));
		}
		if(a[0] == wireThree && a[1] == switchTwo){
			//switchTwo.setBackground(red);
			remove(label3);
			label3.removeAll();
			label3.setBounds(75,125,490,200);
			label3.setIcon(threeToTwo);
			add(label3, new Integer(4));
			switchTwo.setBackground(green);
			wireThree.setEnabled(false);
			switchTwo.setEnabled(false);
			a[0] = null;
			onOff[2] = true;
		}
		if(a[0] == wireThree && a[1] == switchThree){
			//switchThree.setBackground(green);
			remove(label3);
			label3.removeAll();
			label3.setBounds(75,125,490,200);
			label3.setIcon(threeToThree);
			add(label3, new Integer(4));
		}
		if(onOff[0] == true && onOff[1] == true && onOff[2] == true){
			String message1 = "You Have Solved The Puzzle";
			JOptionPane.showMessageDialog(null, message1,"WINNER", JOptionPane.INFORMATION_MESSAGE);
			if(firstTime == true){
				firstTime = false;
			}
			this.removeAll();
		}
	}
	/*public static void main(String args[]){
		SwingUtilities.invokeLater(new Runnable(){						
			public void run(){	
				createAndShowGUI();
			}
		});
	}
	private static void createAndShowGUI(){
		CircuitButton frame = new CircuitButton();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(new Dimension(640,480));
		frame.setLocationRelativeTo( null );
		frame.setResizable(false);
		frame.setTitle("Circuit Game");
		frame.setVisible(true);

	}*/
	
	public boolean beenPlayed()
	{
		return played;
	}
	
	public void setPlayed(boolean p)
	{
		played = p;
	}
	
	public void tutorial(){
		if(firstTime){
		firstTime = false;
		String temp = "Welcome to your first Minigame\n" + 
		"Here we provide a brief tutorial of the Minigame Environment";
		String line1 = "Always Displayed on the Upper Left will be a detailed description of the\n"
			+ "Minigame rules and objectives";
		String line2 = "On the Upper Right will be Timer keeping your time for the particular Minigame\n"
			+ "Complete the game in the desired time and receive a helpful Hint";
		String line3 = "The Center of the screen is the interactive Minigame where you are challenged\n"
			+ "To complete the task to inorder to advice further into the mansion";
		String line4 = "Complete the puzzles and try to find Dave. Enjoy the adventure!";
		JOptionPane.showMessageDialog(null,temp,"Tutorial", JOptionPane.INFORMATION_MESSAGE);
		JOptionPane.showMessageDialog(null,line1,"Tutorial", JOptionPane.INFORMATION_MESSAGE);
		JOptionPane.showMessageDialog(null,line2,"Tutorial", JOptionPane.INFORMATION_MESSAGE);
		JOptionPane.showMessageDialog(null,line3,"Tutorial", JOptionPane.INFORMATION_MESSAGE);
		JOptionPane.showMessageDialog(null,line4,"Tutorial", JOptionPane.INFORMATION_MESSAGE);
		}
	}
}
